package cn.zldemo;

import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;

import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import cn.zldemo.util.SetOnClickUtil;
import cn.zldemo.util.Utils;
import cn.zldemo.util.ViewMapping;
import cn.zldemo.util.ViewMappingUtil;

@ViewMapping(id = R.layout.activity_status_bar)
public class StatusBarActivity extends AppCompatActivity implements View.OnClickListener {
	@ViewMapping(id = R.id.btn1)
	Button btn1;
	@ViewMapping(id = R.id.btn2)
	Button btn2;
	@ViewMapping(id = R.id.btn3)
	Button btn3;
	@ViewMapping(id = R.id.btn4)
	Button btn4;
	@ViewMapping(id = R.id.btn5)
	Button btn5;
	@ViewMapping(id = R.id.btn6)
	Button btn6;
	@ViewMapping(id = R.id.btn7)
	Button btn7;
	@ViewMapping(id = R.id.btn8)
	Button btn8;
	@ViewMapping(id = R.id.btn9)
	Button btn9;
	@ViewMapping(id = R.id.btn10)
	Button btn10;
	@ViewMapping(id = R.id.btn11)
	Button btn11;
	@ViewMapping(id = R.id.btn12)
	Button btn12;

	private View view;
	Palette.Builder build;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewMappingUtil.mapView(this, this);
		SetOnClickUtil.registerOnClick(this);
		view = getWindow().getDecorView();
		build = Palette.from(((BitmapDrawable) getResources().getDrawable(R.mipmap.ic_launcher)).getBitmap())
				.maximumColorCount(16).resizeBitmapSize(192);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn1:
			view.setSystemUiVisibility(0);
			getWindow().clearFlags(FLAG_TRANSLUCENT_NAVIGATION);
			getWindow().clearFlags(FLAG_TRANSLUCENT_STATUS);
			break;
		case R.id.btn2:
			setUI(View.SYSTEM_UI_FLAG_FULLSCREEN);
			break;
		case R.id.btn3:
			setUI(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
			break;
		case R.id.btn4:
			setUI(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
			break;
		case R.id.btn5:
			setUI(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
			break;
		case R.id.btn6:
			setUI(View.SYSTEM_UI_FLAG_IMMERSIVE);
			break;
		case R.id.btn7:
			setUI(View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
			break;
		case R.id.btn8:
			build.generate(new Palette.PaletteAsyncListener() {
				@Override
				public void onGenerated(Palette palette) {
					Palette.Swatch swatch = palette.getVibrantSwatch();
					int tc = swatch.getTitleTextColor(), bc = swatch.getBodyTextColor();
					if (Utils.hasLollipop()) {
						getWindow().setStatusBarColor(swatch.getRgb());
					}
				}
			});
			break;
		case R.id.btn9:
			int color = build.generate().getVibrantColor(Color.YELLOW);
			if (Utils.hasLollipop()) {
				getWindow().setNavigationBarColor(color);
			}
			break;
		case R.id.btn10:
			btn10.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
			break;
		case R.id.btn11:
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			break;
		case R.id.btn12:
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			break;
		}
	}

	private void setUI(int id) {
		int UiOptions = view.getSystemUiVisibility() | id;
		view.setSystemUiVisibility(UiOptions);
	}
}
